<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{css/public.css}" media="all">
</head>
<body  style="margin: 0;background-color:#fff;">
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" action="">

            <input name="id" type="hidden"  th:value="${role.getId()}">
            <input name="status" type="hidden"  th:value="${role.getStatus()}">

            <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-block">
                    <input type="text"  name="roleName" th:value="${role.getRoleName()}" lay-verify="title" autocomplete="off" placeholder="请输入角色名称" class="layui-input">
                </div>
            </div>
            <!--<div class="layui-form-item layui-form-text">
              <label class="layui-form-label">编辑器</label>
              <div class="layui-input-block">
                <textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="LAY_demo_editor"></textarea>
              </div>
            </div>-->
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>


    </div>
</div>

<script th:src="@{lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;
        var $=layui.jquery;

        //监听提交
        form.on('submit(demo1)', function (data) {

            //正式添加之前 做判断  当前输入的这个角色名字 是否出现在数据库中，如果出现 则不允许添加
            $.ajax({
                url:"/countRoleByName",
                type:"post",
                data:{roleName:data.field.roleName},
                success:function (res) {
                    if (res.code=="0000"){
                        //异步添加方法
                        $.ajax({
                            url:"/saveRole",
                            type:"post",
                            data:data.field,
                            success:function (res) {
                                if (res.code=="0000"){
                                    //关闭当前弹出层并且刷新父级页面
                                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                                    parent.close(iframeIndex);
                                    window.parent.location.reload();
                                }
                            }
                        })
                    }else {
                        layer.msg(res.msg);
                    }
                }
            })

            return false;
        });



    });
</script>

</body>
</html>